package priv.jesse.mall.web.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import priv.jesse.mall.entity.*;
import priv.jesse.mall.entity.pojo.ResultBean;
import priv.jesse.mall.service.OrderService;
import priv.jesse.mall.service.ShopCartService;
import priv.jesse.mall.service.exception.LoginException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/newOrder")
public class OrderNewController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ShopCartService shopCartService;
    /**
     * 打开订单列表页面
     *
     * @return
     */
    @RequestMapping("/toList.html")
    public String toOrderList() {
        return "mall/order/list";
    }

    /**
     * 查询用户订单列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/list.do")
    @ResponseBody
    public ResultBean<List<Order>> listData(HttpServletRequest request) {
        List<Order> orders = orderService.findUserOrder(request);
        return new ResultBean<>(orders);
    }

    /**
     * 查询订单详情
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/getDetail.do")
    @ResponseBody
    public ResultBean<List<OrderItem>> getDetail(int orderId) {
        List<OrderItem> orderItems = orderService.findItems(orderId);
        return new ResultBean<>(orderItems);
    }

    /**
     * 检查是否登录
     */
    /*@RequestMapping("/inspectdl.do")
    @ResponseBody
    public boolean inspectdl(HttpServletRequest request) throws Exception {
        Object user = request.getSession().getAttribute("user");
        if (user == null){
            return false;
        }
        return true;
    }*/


    /**
     * 新增收货地址
     */
    @RequestMapping("/addadress.do")
    public void addadress(String dizhi, HttpServletRequest request) throws Exception {
        orderService.addadress(dizhi,request);
    }

    /**
     * 提交订单
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping("/addOrder.do")
    public ResultBean<Integer> addOrder(String shopCartStr,String dizhi,String totalPrice, HttpServletRequest request, HttpServletResponse response) throws Exception {
        int i=orderService.addOrder(shopCartStr, dizhi,totalPrice, request);
        return new ResultBean<>(i);
    }

    /**
     * 支付方法
     *
     * @param orderId
     */
    @RequestMapping("pay.do")
    @ResponseBody
    public ResultBean<Boolean> pay(int orderId, HttpServletResponse response) throws IOException {
        orderService.pay(orderId);
        return new ResultBean<>(true);
    }

    /**
     * 确认收货
     * @param orderId
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("receive.do")
    @ResponseBody
    public ResultBean<Boolean> receive(int orderId, HttpServletResponse response) throws IOException {
        orderService.receive(orderId);
        return new ResultBean<>(true);
    }




    /**
     * 加载购物车数量
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadShopcount.do")
    public ResultBean<Integer> loadShopcount(HttpServletRequest request) throws Exception {
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null) {
            return new ResultBean<>(-1);
        }
        Integer i=shopCartService.loadShopcount(request);
        return new ResultBean<>(i);
    }

    /**
     * 加购物车并跳转购物车页面
     *
     * @param productId
     * @param request
     * @return
     */
    @RequestMapping("/newaddCart.do")
    public String newaddToCart(int productId, int prodCount,HttpServletRequest request,Map<String, Object> map) throws Exception {
        shopCartService.newaddCart(productId,prodCount, request);
        List<OrderItem> Shopcart =shopCartService.loadlistCart(request);
        map.put("Shopcart", Shopcart);
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null) {
            throw new Exception("未登录！请重新登录");
        }
        map.put("userid", loginUser.getId());
        return "newmall/shopcart";
    }


    /**
     * 加载购物车
     * @return
     */
    @ResponseBody
    @RequestMapping("/listCart.do")
    public ResultBean<List<OrderItem>> loadlistCart(HttpServletRequest request) throws Exception {
        List<OrderItem> Shopcart =shopCartService.loadlistCart(request);
        return new ResultBean<>(Shopcart);
    }


    /**
     * 打开购物车
     * @return
     */
    @RequestMapping("/shopcart.html")
    public String toShopcart(HttpServletRequest request,Map<String, Object> map) throws Exception{
        List<OrderItem> Shopcart =shopCartService.loadlistCart(request);
        map.put("Shopcart", Shopcart);
        User loginUser = (User) request.getSession().getAttribute("user");
        if (loginUser == null) {
            throw new Exception("未登录！请重新登录");
        }
        map.put("userid", loginUser.getId());
        return "newmall/shopcart";
    }


    /**
     * 删除购物车
     * @return
     */
    @ResponseBody
    @RequestMapping("/shortdelete.do")
    public ResultBean<Integer> shortdelete(HttpServletRequest request,int shopcartid) throws Exception{
        int i=shopCartService.shortdelete(request,shopcartid);
        return new ResultBean<>(i);
    }

    /**
     * 更新购物车数量
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateshortcount.do")
    public ResultBean<Integer> updateshortcount(HttpServletRequest request,int cartid,int count) throws Exception{
        int i=shopCartService.updateshortcount(request,cartid,count);
        return new ResultBean<>(i);
    }

    /**
     * 加载用户地址
     * @return
     */
    @ResponseBody
    @RequestMapping("/loadeadress.do")
    public ResultBean<List<Address>> loadeadress(int userid) throws Exception{
        List<Address>  list=shopCartService.loadeadress(userid);
        return new ResultBean<>(list);
    }


    /**
     * 添加与修改用户地址
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateadress.do")
    public ResultBean<Integer> updateadress(HttpServletRequest request,String addressss) throws Exception{
        Integer  i=shopCartService.updateadress(request,addressss);
        return new ResultBean<>(i);
    }


    /**
     * 打开付款页
     * @return
     */
    @RequestMapping("/payment.html")
    public String payment(Integer shortid,HttpServletRequest request,Map<String, Object> map) throws Exception{
        Order order =shopCartService.payment(request,shortid);
        map.put("order",order);
        return "newmall/order";
    }













}
